<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <title>Google Maps JavaScript API v3 Example: Reverse Geocoding</title>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
            var geocoder;
            var map;
            var infowindow = new google.maps.InfoWindow();
            var marker;
            function initialize() {
                geocoder = new google.maps.Geocoder();
                var latlng = new google.maps.LatLng(40.730885,-73.997383);
                var myOptions = {
                    zoom: 8,
                    center: latlng,
                    mapTypeId: 'roadmap'
                }
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            }

            function codeLatLng() {
                var input = document.getElementById("latlng").value;
                var latlngStr = input.split(",",2);
                var lat = latlngStr[0];
                var lng = latlngStr[1];
                var latlng = new google.maps.LatLng(lat, lng);
                if (geocoder) {
                    geocoder.geocode({'latLng': latlng}, function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            if (results[1]) {
                                map.setZoom(11);
                                marker = new google.maps.Marker({
                                    position: latlng,
                                    map: map
                                });
                                infowindow.setContent(results[1].formatted_address);
                                infowindow.open(map, marker);
                            } else {
                                alert("No results found");
                            }
                        } else {
                            alert("Geocoder failed due to: " + status);
                        }
                    });
                }
            }
        </script>
    </head>
    <body style="margin:0px; padding:0px;" onload="initialize()">
        <div>
            <input id="latlng" type="textbox" value="40.714224,-73.961452">
        </div>
        <div>
            <input type="button" value="Reverse Geocode" onclick="codeLatLng()">
        </div>
        <div>
            <div id="map_canvas" style="width: 100%; height: 90%; float:left; border: 1px solid black;"></div>
        </div>
    </body>
</html>
